package com.skyc.dao.impl;

import java.sql.ResultSet;

import javax.sql.rowset.CachedRowSet;
import com.skyc.dao.AdminDao;
import com.skyc.entity.Admin;
import com.skyc.entity.Clothes;
import com.skyc.entity.Order;
import com.skyc.util.JDBCUtil;

public class AdminDaoImpl implements AdminDao {
	JDBCUtil util=JDBCUtil.getJDBCUtil();
	@Override
	public boolean updateIUPE(Admin admin) {
		return util.update("update t_admin set a_imgurl=?,a_name=?,a_phone=?,a_email=? where a_adminid=?",
				admin.getAimgurl(),admin.getAname(),admin.getAphone(),admin.getAemail(),admin.getAadminid());
	}
	@Override
	public Admin queryByid(int id) {
		 String sql = "select * from t_admin where a_adminid=?";
			
			System.out.println(sql);
			CachedRowSet rs= util.query(sql,id);
			try {
				while(rs.next()) {
					Admin a=new Admin();
					a.setAadminid(rs.getInt("a_adminid"));
					a.setAname(rs.getString("a_name"));
					a.setApwd(rs.getString("a_pwd"));
					a.setAphone(rs.getString("a_phone"));
					a.setAemail(rs.getString("a_email"));
					a.setAimgurl(rs.getString("a_imgurl"));
					return a;
				}
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally {
				util.closeCacheRowSet(rs);
			}
			return null;
	}
	@Override
	public boolean updatePWD(Admin admin) {
		return util.update("update t_admin set a_pwd=? where a_adminid=?",
				admin.getApwd(),admin.getAadminid());
	}
	@Override
	public boolean save(Clothes c) {
		String sql="insert into t_clothes(c_cid,c_name,c_type,c_imgurl,c_stock,c_price,c_sales,c_sex,c_color,c_size,c_maintype,c_shopid) values (?,?,?,?,?,?,?,?,?,?,?,?)";
		return util.update(sql,c.getCcid(),c.getCname(),c.getCtype(),c.getCimgurl(),c.getCstock(),c.getCprice(),c.getCsales(),c.getCsex(),c.getCcolor(),c.getCsize(),c.getCmaintype(),c.getCshopid());
	}
	@Override
	public boolean login(String name, String pwd) {
		boolean b=false;
		CachedRowSet rs= util.query("select * from t_admin where a_name=? and a_pwd=? ", name,pwd);
		try{
			if(rs.next()){
	                    b = true;
	                }else {
	                	b=false;
	                }
			
			return b;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			util.closeCacheRowSet(rs);
		}	
		return false;
	}
	@Override
	public boolean queryCShopid(String cshopid) {
		boolean b=false;
		CachedRowSet rs= util.query("select * from t_clothes where c_shopid=? ", cshopid);
		try{
			if(rs.next()){
                b = true;
            }else {
            	b=false;
            }
	
	      return b;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			util.closeCacheRowSet(rs);
		}	
		return false;
	}
	@Override
	public boolean deteleByShopID(String cshopid) {
		// TODO Auto-generated method stub
		return util.update("DELETE FROM t_clothes WHERE c_shopid=?", cshopid);
	}
	@Override
	public boolean updateStock(Clothes c) {
		return util.update("update t_clothes set c_stock=? where c_shopid=?",
				c.getCstock(),c.getCshopid());
	}
	@Override
	public Clothes query(String cshopid) {
		 String sql = "select * from t_clothes where c_shopid=?";
			
			System.out.println(sql);
			CachedRowSet rs= util.query(sql,cshopid);
			try {
				while(rs.next()) {
					Clothes c=new Clothes();
					c.setCcolor(rs.getString("c_color"));
					c.setCid(rs.getInt("c_id"));
					c.setCcid(rs.getInt("c_cid"));
					c.setCimgurl(rs.getString("c_imgurl"));
					c.setCmaintype(rs.getString("c_maintype"));
					c.setCname(rs.getString("c_name"));
					c.setCprice(rs.getInt("c_price"));
					c.setCsales(rs.getInt("c_sales"));
					c.setCsex(rs.getString("c_sex"));
					c.setCsize(rs.getString("c_size"));
					c.setCstock(rs.getInt("c_stock"));
					c.setCtype(rs.getString("c_type"));
					c.setCshopid(rs.getString("c_shopid"));
					return c;
					
				}
				
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}finally {
				util.closeCacheRowSet(rs);
			}
			return null;
	}

}
